package com.dao;

import com.pojo.Grade;
import com.utils.JdbcUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @CreateTime: 2022-12-24  16:05
 * @Description: TODO
 * @Version: 1.0.0
 */
public class IGradeDAOImpl implements IGradeDAO {

    private static Connection connection;

    static {
        try {
            // 拿到连接
            connection = JdbcUtil.getConnection();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Grade> findAllList() {
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = connection.prepareStatement("select * from tb_grade");
            ResultSet resultSet = preparedStatement.executeQuery();
            List<Grade> grades = new ArrayList<>();
            while (resultSet.next()) {
                Grade grade = new Grade(resultSet.getInt("id"), resultSet.getString("grade_name"), resultSet.getString("grade_comment"));
                grades.add(grade);
            }
            return grades;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }

    public static void main(String[] args) {
        List<Grade> allList = new IGradeDAOImpl().findAllList();
        System.out.println(allList);
    }


}
